package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.ProdProp;
import com.powernode.domain.ProdPropValue;
import com.powernode.model.Result;
import com.powernode.service.ProdPropService;
import com.powernode.service.ProdPropValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: Tong Ziyu
 * @Date: 2025/2/22 18:26
 */
@RestController
@RequestMapping("prod/spec")
@Api(tags = "商品属性接口管理")
@Slf4j
public class ProdSpecController {

    @Autowired
    private ProdPropService prodPropService;

    @Autowired
    private ProdPropValueService prodPropValueService;

    /**
     * 分页查询所有属性
     * @param propName 关键字
     * @param current 当前页
     * @param size  每页多少条
     * @return
     */
    @ApiOperation("分页查询所有属性")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public Result<Page<ProdProp>> loadProdSpecPage(
            @RequestParam(value = "propName", required = false) String propName,
            @RequestParam("current") Long current,
            @RequestParam("size") Long size
    ){
        Page<ProdProp> result =  prodPropService.queryProdPropPage(propName,current,size);

        return Result.success(result);
    }

    /**
     * 保存产品规格
     * @return
     */
    @ApiOperation("新增属性规格")
    @PostMapping()
    @PreAuthorize("hasAuthority('prod:spec:save')")
    public Result<String> saveProdSpec(@RequestBody ProdProp prodProp){
        log.info("新增属性规格:{}",prodProp);
        Boolean i = prodPropService.saveProdSpec(prodProp);
        return Result.handle(i);
    }

    /**
     * 修改属性规格
     * @param prodProp
     * @return
     */
    @ApiOperation("修改属性规格")
    @PutMapping()
    @PreAuthorize("hasAuthority('prod:spec:update')")
    public Result<String> updateProdPropById(@RequestBody ProdProp prodProp){
        log.info("修改属性规格:{}",prodProp);
        Boolean b = prodPropService.updateProdPropById(prodProp);
        return Result.handle(b);
    }

    /**
     * 根据prodPropId删除属性
     * @return
     */
    @ApiOperation("根据prodPropId删除属性")
    @DeleteMapping("{prodPropId}")
    @PreAuthorize("hasAuthority('prod:spec:delete')")
    public Result<String> deleteProdPropById(@PathVariable(value = "prodPropId") Long prodPropId){
        Boolean b = prodPropService.deleteByProdPropId(prodPropId);

        return Result.handle(b);
    }

    /**
     * 查看商品规格
     * @return
     */
    @ApiOperation("查看商品规格")
    @GetMapping("list")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public Result<List<ProdProp>> loadSpecList(){
        return Result.success(prodPropService.queryProdPropList());
    }

    /**
     * 根据商品规格id查询属性值
     * @param valueId
     * @return
     */
    @ApiOperation("根据商品规格id查询属性值")
    @GetMapping("listSpecValue/{valueId}")
    @PreAuthorize("hasAuthority('prod:spec:info')")
    public Result<List<ProdPropValue>> loadProdPropValueById(@PathVariable("valueId") Long valueId){

        return Result.success(prodPropValueService.list(
                new LambdaQueryWrapper<ProdPropValue>()
                        .eq(ProdPropValue::getPropId, valueId)
        ));
    }
}
